<?php

# http://projects:8081/cdapp-2kt-api/api/sipds/load_flight_dynamic.php

include_once '__sipds.php';

// following constants for short test
// const FLIGHT_DYNAMIC_AIRPORT_CODES = ['ZUUU'];
// const FLIGHT_DYNAMIC_TYPE_LIST = ['arr'];
// const FLIGHT_DYNAMIC_DATE_OFFSET_LIST = [0];

// constants
const FLIGHT_DYNAMIC_AIRPORT_CODES = ['ZUUU', 'ZUTF', 'ZUMY', 'ZUYB', 'ZULZ'];
const FLIGHT_DYNAMIC_TYPE_LIST = ['arr', 'dep'];
const FLIGHT_DYNAMIC_DATE_OFFSET_LIST = [0, 1];

// request url
const FLIGHT_DYNAMIC_URL = 'http://' . SIPDS_IP . ':10080/SIPDS/ws/soap/flightDynamicWebService';

// request payload
const FLIGHT_DYNAMIC_PAYLOAD_EXAMPLE = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><tns:RequestSOAPHeader xmlns:tns="http://sipds.adcc.cn"><tns:mac xmlns:tns="x">00-D8-61-3C-5E-61</tns:mac><tns:seatId xmlns:tns="x">168</tns:seatId><tns:terminalId xmlns:tns="x">164</tns:terminalId></tns:RequestSOAPHeader></s:Header><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><findDynaPlan xmlns="http://sipds.adcc.cn"><type xmlns="">__DATE__</type><predicate xsi:type="q1:CompoundPredicate" xmlns="" xmlns:q1="http://sipds.adcc.cn"><expressions><expression xsi:type="q1:CompoundPredicate"><expressions><expression xsi:type="q1:NullnessPredicate"><negated>false</negated><operand xsi:type="q1:AttributePath"><attribute>delDeps</attribute></operand></expression><expression xsi:type="q1:ComparisonPredicate"><negated>false</negated><comparisonOperator>EQUAL</comparisonOperator><leftHandSide xsi:type="q1:AttributePath"><attribute>delDeps</attribute></leftHandSide><rightHandSide xsi:type="q1:LiteralExpression"><literal xsi:type="xsd:string" /></rightHandSide></expression></expressions><negated>false</negated><operator>OR</operator></expression><expression xsi:type="q1:LikePredicate"><negated>false</negated><matchExpression xsi:type="q1:AttributePath"><attribute>__TYPE__</attribute></matchExpression><pattern xsi:type="q1:LiteralExpression"><literal xsi:type="xsd:string">%__CODE__%</literal></pattern></expression></expressions><negated>false</negated><operator>AND</operator></predicate></findDynaPlan></s:Body></s:Envelope>';
const FLIGHT_DYNAMIC_PARAM_KEYS = ['date' => '__DATE__', 'type' => '__TYPE__', 'code' => '__CODE__'];

function load_flight_dynamic() {
  $url = FLIGHT_DYNAMIC_URL;
  $content = [];
  // variables for test
  $payload_list = [];
  $response_list = [];
  $sql_list = [];
  $result_list = [];
  // get payloads
  foreach (FLIGHT_DYNAMIC_AIRPORT_CODES as $code) {
    foreach (FLIGHT_DYNAMIC_DATE_OFFSET_LIST as $date) {
      foreach (FLIGHT_DYNAMIC_TYPE_LIST as $type) {
        $payload = FLIGHT_DYNAMIC_PAYLOAD_EXAMPLE;
        $payload = str_replace(FLIGHT_DYNAMIC_PARAM_KEYS['code'], $code, $payload);
        $payload = str_replace(FLIGHT_DYNAMIC_PARAM_KEYS['date'], $date, $payload);
        $payload = str_replace(FLIGHT_DYNAMIC_PARAM_KEYS['type'], $type, $payload);
        $payload_list[] = $payload;
        $res = request_sipds($url, $payload);
        if ($res && $res['status'] == 1 && $res['data'] && is_array($res['data'])) {
          $response_list[] = $res['data'];
          foreach ($res['data'] as $flight) {
            $cell = [];
            // output($flight);
            // file_put_contents('load_flight_dynamic_result.txt', json_stringify($flight) . "\n\n", FILE_APPEND);
            $cell['ACTYPE'] = $ACTYPE = isset($flight["ACTYPE"]) ? $flight["ACTYPE"] : ''; // 机型
            $cell['ARR'] = $ARR = isset($flight["ARR"]) ? $flight["ARR"] : ''; // 落地机场ICAO
            $cell['ARRNAME'] = $ARRNAME = isset($flight["ARRNAME"]) ? $flight["ARRNAME"] : ''; // 落地机场名称
            $cell['ATA'] = $ATA = isset($flight["ATA"]) ? $flight["ATA"] : ''; // 实际落地
            $cell['ATD'] = $ATD = isset($flight["ATD"]) ? $flight["ATD"] : ''; // 实际起飞
            $cell['ATTRI2'] = $ATTRI2 = isset($flight["ATTRI2"]) ? $flight["ATTRI2"] : ''; // 导航通讯
            $cell['ATTRI3'] = $ATTRI3 = isset($flight["ATTRI3"]) ? $flight["ATTRI3"] : ''; // 监视设备
            $cell['ATTRI4'] = $ATTRI4 = isset($flight["ATTRI4"]) ? $flight["ATTRI4"] : ''; // 备降机场
            $cell['ATTRI5'] = $ATTRI5 = isset($flight["ATTRI5"]) ? $flight["ATTRI5"] : ''; // 备降机场

            $cell['CHANGEDFLAG'] = $CHANGEDFLAG = isset($flight["CHANGEDFLAG"]) ? $flight["CHANGEDFLAG"] : ''; // ???
            $cell['DELAYTIME'] = $DELAYTIME = isset($flight["DELAYTIME"]) ? $flight["DELAYTIME"] : ''; // 已延误?
            $cell['DEP'] = $DEP = isset($flight["DEP"]) ? $flight["DEP"] : ''; // 起飞机场ICAO
            $cell['DEPNAME'] = $DEPNAME = isset($flight["DEPNAME"]) ? $flight["DEPNAME"] : ''; // 起飞机场名称
            $cell['DOF'] = $DOF = isset($flight["DOF"]) ? $flight["DOF"] : ''; // 航班日期?
            $cell['DYNAETA'] = $DYNAETA = isset($flight["DYNAETA"]) ? $flight["DYNAETA"] : ''; // 动态ETA
            $cell['DYNAETD'] = $DYNAETD = isset($flight["DYNAETD"]) ? $flight["DYNAETD"] : ''; // 动态ETD
            $cell['DYNAPLANID'] = $DYNAPLANID = isset($flight["DYNAPLANID"]) ? $flight["DYNAPLANID"] : ''; // 动态计划ID?
            $cell['FLTHEIGHT'] = $FLTHEIGHT = isset($flight["FLTHEIGHT"]) ? $flight["FLTHEIGHT"] : ''; // 巡航高度
            $cell['FLTID'] = $FLTID = isset($flight["FLTID"]) ? $flight["FLTID"] : ''; // 航班号
            $cell['FLTNUM'] = $FLTNUM = isset($flight["FLTNUM"]) ? $flight["FLTNUM"] : ''; // 注册号
            $cell['FLTSTATE'] = $FLTSTATE = isset($flight["FLTSTATE"]) ? $flight["FLTSTATE"] : ''; // 航班状态
            $cell['FPLROUTE'] = $FPLROUTE = isset($flight["FPLROUTE"]) ? $flight["FPLROUTE"] : ''; // 计划航路
            $cell['G18'] = $G18 = isset($flight["G18"]) ? $flight["G18"] : ''; // 编组18
            $cell['INPOINT'] = $INPOINT = isset($flight["INPOINT"]) ? $flight["INPOINT"] : ''; // 入境点
            $cell['ISNOTIFYAF'] = $ISNOTIFYAF = isset($flight["ISNOTIFYAF"]) ? $flight["ISNOTIFYAF"] : ''; // ???
            $cell['NONSKEDPLANREF'] = $NONSKEDPLANREF = isset($flight["NONSKEDPLANREF"]) ? $flight["NONSKEDPLANREF"] : ''; // ???
            $cell['PBN'] = $PBN = isset($flight["PBN"]) ? $flight["PBN"] : ''; // PBN能力
            $cell['PLANETA'] = $PLANETA = isset($flight["PLANETA"]) ? $flight["PLANETA"] : ''; // 计划ETA
            $cell['PLANETD'] = $PLANETD = isset($flight["PLANETD"]) ? $flight["PLANETD"] : ''; // 计划ETD
            $cell['PLANID'] = $PLANID = isset($flight["PLANID"]) ? $flight["PLANID"] : ''; // 计划ID?
            $cell['PLANROUTE'] = $PLANROUTE = isset($flight["PLANROUTE"]) ? $flight["PLANROUTE"] : ''; // 计划航路?
            $cell['RELATEDEPS'] = $RELATEDEPS = isset($flight["RELATEDEPS"]) ? $flight["RELATEDEPS"] : ''; // ???
            $cell['RNAV5'] = $RNAV5 = isset($flight["RNAV5"]) ? $flight["RNAV5"] : ''; // ???
            $cell['ROUTE'] = $ROUTE = isset($flight["ROUTE"]) ? $flight["ROUTE"] : ''; // 航路?
            $cell['RVSM'] = $RVSM = isset($flight["RVSM"]) ? $flight["RVSM"] : ''; // RVSM能力
            $cell['SKEDPLANREF'] = $SKEDPLANREF = isset($flight["SKEDPLANREF"]) ? $flight["SKEDPLANREF"] : ''; // ???
            $cell['SPEED'] = $SPEED = isset($flight["SPEED"]) ? $flight["SPEED"] : ''; // 巡航速度
            $cell['SSRCALLBACK'] = $SSRCALLBACK = isset($flight["SSRCALLBACK"]) ? $flight["SSRCALLBACK"] : ''; // ???
            $cell['SSRCODE'] = $SSRCODE = isset($flight["SSRCODE"]) ? $flight["SSRCODE"] : ''; // 应答机编码
            $cell['SSRMODE'] = $SSRMODE = isset($flight["SSRMODE"]) ? $flight["SSRMODE"] : ''; // 应答机模式
            $cell['STATUSCHANGEDTIME'] = $STATUSCHANGEDTIME = isset($flight["STATUSCHANGEDTIME"]) ? $flight["STATUSCHANGEDTIME"] : ''; // 状态变更时间
            $cell['SYSMEMO'] = $SYSMEMO = isset($flight["SYSMEMO"]) ? $flight["SYSMEMO"] : ''; // ???
            $cell['TASK'] = $TASK = isset($flight["TASK"]) ? $flight["TASK"] : ''; // 飞行种类
            $cell['UNCONFIRMED'] = $UNCONFIRMED = isset($flight["UNCONFIRMED"]) ? $flight["UNCONFIRMED"] : ''; // ???
            $cell['USERMEMO'] = $USERMEMO = isset($flight["USERMEMO"]) ? $flight["USERMEMO"] : ''; // ???
            $cell['WEEKDAYS'] = $WEEKDAYS = isset($flight["WEEKDAYS"]) ? $flight["WEEKDAYS"] : ''; // 执行周期
            $cell['ZBAPPROVAL'] = $ZBAPPROVAL = isset($flight["ZBAPPROVAL"]) ? $flight["ZBAPPROVAL"] : ''; // ???
            $cell['_identifier'] = $identifier = (string)$DYNAPLANID . '_' . (string)$PLANID . '_' . $FLTID . '_' . $DEP . '_' . $ARR;
            $content[] = $cell;
          }
        }
      }
    }
  }

}

load_flight_dynamic();
